﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.BLL;
using RFIDStockManagement.Common.Public.Enums;

namespace RFIDStockManagement.Client
{
    public partial class ItemEdit : XtraForm
    {
        private ItemInfo m_ItemInfo = null;
        private long m_Id = 0;

        public ItemEdit(long id)
        {
            InitializeComponent();
            m_Id = id;
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                #region 验证数据
                if (txtName.Text.Trim().Length == 0 || txtNumber.Text.Trim().Length == 0)
                {
                    AlertForm.ShowMessage("名称、编码不能为空");
                    return;
                }
                int iValidMonth = 0;
                if (txtValidMonth.Text.Trim().Length > 0 && int.TryParse(txtValidMonth.Text.Trim(), out iValidMonth) == false)
                {
                    AlertForm.ShowMessage("有效期月不合法");
                    return;
                }
                if (cbbStock.SelectedIndex < 0 || cbbStockPlace.SelectedIndex < 0)
                {
                    AlertForm.ShowMessage("默认仓库、默认仓位不能为空");
                    return;
                }
                if (cbbMeasureGroup.SelectedIndex < 0 || cbbStockMeasure.SelectedIndex < 0)
                {
                    AlertForm.ShowMessage("计量单位组、默认库存单位不能为空");
                    return;
                }
                decimal decSafeStock = 0;
                if (txtSafeStock.Text.Trim().Length > 0 && decimal.TryParse(txtSafeStock.Text.Trim(), out decSafeStock) == false)
                {
                    AlertForm.ShowMessage("安全库存不合法");
                    return;
                }
                #endregion
                if (m_ItemInfo == null)
                {
                    m_ItemInfo = new ItemInfo();
                }
                m_ItemInfo.Name = txtName.Text.Trim();
                m_ItemInfo.Number = txtNumber.Text.Trim();
                m_ItemInfo.BatchNo = txtBatchNo.Text.Trim();
                m_ItemInfo.ValidMonth = iValidMonth;
                m_ItemInfo.Type = (EnumItemType)cbbItemType.SelectedIndex;
                m_ItemInfo.SafeStock = decSafeStock;
                m_ItemInfo.BaseRfid = txtBaseRfid.Text.Trim();
                m_ItemInfo.MeasureGroupId = m_MeasureGroupIndex[cbbMeasureGroup.SelectedIndex];
                m_ItemInfo.StockMeasureId = m_MeasureIndex[cbbStockMeasure.SelectedIndex];
                m_ItemInfo.StockId = m_StockIndex[cbbStock.SelectedIndex];
                m_ItemInfo.StockPlaceId = m_StockPlaceIndex[cbbStockPlace.SelectedIndex];

                BasicInfoManager.Save(m_ItemInfo, EnumBasicInfo.Item);
                AlertForm.ShowMessage("保存成功");
                this.Close();
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
            }
        }

        private void ItemEdit_Load(object sender, EventArgs e)
        {
            if (m_Id > 0)
            {
                BindData(m_Id);
                BindMeasureGroupList(m_ItemInfo.MeasureGroupId);
                BindStockList(m_ItemInfo.StockId);
            }
            else
            {
                BindMeasureGroupList(0);
                BindStockList(0);
            }
        }
        private void BindData(long id)
        {
            try
            {
                m_ItemInfo = BasicInfoManager.GetOne(id, EnumBasicInfo.Item) as ItemInfo;
                if (m_ItemInfo == null)
                {
                    AlertForm.ShowMessage("读取不出数据，可能被删除");
                    this.Close();
                    return;
                }
                txtName.Text = m_ItemInfo.Name;
                txtNumber.Text = m_ItemInfo.Number;
                txtBatchNo.Text = m_ItemInfo.BatchNo;
                txtValidMonth.Text = m_ItemInfo.ValidMonth.ToString();
                cbbItemType.SelectedIndex = (int)m_ItemInfo.Type;
                txtSafeStock.Text = m_ItemInfo.SafeStock.ToString("F4");
                txtBaseRfid.Text = m_ItemInfo.BaseRfid;
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
                this.Close();
            }
        }
        #region 初始化仓库、计量单位组
        private Dictionary<int, long> m_MeasureGroupIndex = new Dictionary<int, long>();
        private void BindMeasureGroupList(long mGroupId)
        {
            DataTable dtMeasureGroupList = BasicInfoManager.GetList(null, EnumBasicInfo.MeasureGroup);
            if (dtMeasureGroupList != null)
            {
                cbbMeasureGroup.Properties.BeginUpdate();
                foreach (DataRow row in dtMeasureGroupList.Rows)
                {
                    int index = cbbMeasureGroup.Properties.Items.Add(string.Format("{0}({1})", row["FName"], row["FNumber"]));
                    long id = long.Parse(row["FID"].ToString());
                    m_MeasureGroupIndex[index] = id;
                    if (id == mGroupId)
                    {
                        cbbMeasureGroup.SelectedIndex = index;
                    }
                }
                cbbMeasureGroup.Properties.EndUpdate();
            }
        }
        private Dictionary<int, long> m_MeasureIndex = null;
        private void cbbMeasureGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            long groupId;
            m_MeasureIndex = new Dictionary<int, long>();
            cbbStockMeasure.Properties.Items.Clear();
            cbbStockMeasure.SelectedIndex = -1;
            if (m_MeasureGroupIndex.TryGetValue(cbbMeasureGroup.SelectedIndex, out groupId))
            {
                Dictionary<string, object> filter = new Dictionary<string, object>();
                filter.Add("FMeasureGroupID", groupId);
                DataTable dtMeasure = BasicInfoManager.GetList(filter, EnumBasicInfo.Measure);
                if (dtMeasure != null)
                {
                    cbbStockMeasure.Properties.BeginUpdate();
                    foreach (DataRow row in dtMeasure.Rows)
                    {
                        int index = cbbStockMeasure.Properties.Items.Add(string.Format("{0}({1})", row["FName"], row["FNumber"]));
                        long id = long.Parse(row["FID"].ToString());
                        m_MeasureIndex[index] = id;
                        if (m_ItemInfo != null && m_ItemInfo.StockMeasureId == id)
                        {
                            cbbStockMeasure.SelectedIndex = index;
                        }
                    }
                    cbbStockMeasure.Properties.EndUpdate();
                }
            }
        }

        private Dictionary<int, long> m_StockIndex = new Dictionary<int, long>();
        private void BindStockList(long stockId)
        {
            DataTable dtStockList = BasicInfoManager.GetList(null, EnumBasicInfo.Stock);
            if (dtStockList != null)
            {
                cbbStock.Properties.BeginUpdate();
                foreach (DataRow row in dtStockList.Rows)
                {
                    int index = cbbStock.Properties.Items.Add(string.Format("{0}({1})", row["FName"], row["FNumber"]));
                    long id = long.Parse(row["FID"].ToString());
                    m_StockIndex[index] = id;
                    if (id == stockId)
                    {
                        cbbStock.SelectedIndex = index;
                    }
                }
                cbbStock.Properties.EndUpdate();
            }
        }
        private Dictionary<int, long> m_StockPlaceIndex = null;
        private void cbbStock_SelectedIndexChanged(object sender, EventArgs e)
        {
            long stockId;
            m_StockPlaceIndex = new Dictionary<int, long>();
            cbbStockPlace.Properties.Items.Clear();
            cbbStockPlace.SelectedIndex = -1;
            if (m_StockIndex.TryGetValue(cbbStock.SelectedIndex, out stockId))
            {
                Dictionary<string, object> filter = new Dictionary<string, object>();
                filter.Add("FStockID", stockId);
                DataTable dtStockPlace = BasicInfoManager.GetList(filter, EnumBasicInfo.StockPlace);
                if (dtStockPlace != null)
                {
                    cbbStockPlace.Properties.BeginUpdate();
                    foreach (DataRow row in dtStockPlace.Rows)
                    {
                        int index = cbbStockPlace.Properties.Items.Add(string.Format("{0}({1})", row["FName"], row["FNumber"]));
                        long id = long.Parse(row["FID"].ToString());
                        m_StockPlaceIndex[index] = id;
                        if (m_ItemInfo != null && m_ItemInfo.StockPlaceId == id)
                        {
                            cbbStockPlace.SelectedIndex = index;
                        }
                    }
                    cbbStockPlace.Properties.EndUpdate();
                }
            }
        }

        #endregion



    }
}